using UnityEngine;
using Delta2DGame.Base;

namespace Delta2DGame.State
{
    /// <summary>
    /// 跳跃状态
    /// </summary>
    public class JumpState : PlayerState
{
    private Rigidbody2D rb;
    [SerializeField] private float jumpForce = 7f;
    [SerializeField] private float airMoveSpeed = 3f;

    public JumpState(PlayerStateMachine stateMachine, PlayerController player, string animBoolName) : base(stateMachine, player, animBoolName)
    {
        rb = player.GetComponent<Rigidbody2D>();
    }

    // public override void Enter()
    // {
    //     base.Enter();
    //     // 应用跳跃力
    //     rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    // }

    public override void Update()
    {
        base.Update();
        
        // 根据移动输入决定切换到跑步或待机状态
        if (Mathf.Abs(player.GetMoveInput().x) > 0.1f || Mathf.Abs(player.GetMoveInput().y) > 0.1f)
        {
            stateMachine.SwitchState(stateMachine.runState);
        }
        else if (IsAnimationDone())
        {
            stateMachine.SwitchState(stateMachine.idleState);
        }
    }

    private bool IsAnimationDone()
    {
        //判断动画是否播放完毕
        AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0);
        // 检查是否在播放特定动画且动画已播放完成
        if (stateInfo.IsName("jump") && stateInfo.normalizedTime >= 1.0f)
        {
            // 动画播放完成
            return true;
        }
        return false;
    }
    
    

    // public override void FixedUpdate()
    // {
    //     base.FixedUpdate();
    //     // 空中水平移动
    //     float moveInput = player.GetMoveInput().x;
    //     rb.velocity = new Vector2(moveInput * airMoveSpeed, rb.velocity.y);
    // }
}
}